package com.wujilin.okra.loader;

import android.content.AsyncTaskLoader;
import android.content.Context;

/**
 * The abstract loader for loading any data other than a Cursor.
 * 
 * @author Belin Wu
 *
 * @param <D> The type of the data
 */
public abstract class DataLoader<D> extends AsyncTaskLoader<D> {
	private D data;
	
	public DataLoader(Context context) {
		super(context);
	}
	
	@Override
	public void deliverResult(D data) {
		this.data = data;
		if (isStarted()) {
			super.deliverResult(data);
		}
	}
	
	@Override
	protected void onStartLoading() {
		if (data != null) {
			deliverResult(data);
			return;
		}
		forceLoad();
	}
	
	@Override
	protected void onStopLoading() {
		cancelLoad();
		data = null;
	}
}
